package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.ACCID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.HOSTAUDIT;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.IMTOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USERID;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.USER_NAME;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.luoxiang.lib_common.utils.MmkvUtil;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.LoginUtil;
import com.luoxiang.socialandroid.utils.NetUtils;
import com.luoxiang.socialandroid.view.ScrollChangeLisWebView;
import com.netease.yunxin.kit.corekit.im.IMKitClient;
import com.netease.yunxin.kit.corekit.im.login.LoginCallback;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class IdSetActivity extends BaseActivity {
    private RelativeLayout r_heimingdan, relativeLayout;
    private ImageView icon_back;

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_id_set);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        r_heimingdan = findViewById(R.id.r_heimingdan);
        icon_back = findViewById(R.id.icon_back);
        relativeLayout = findViewById(R.id.relativeLayout);
    }

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                finish();
            }

            @Override
            protected void onFastClick() {

            }
        });
        r_heimingdan.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                Intent intent = new Intent(IdSetActivity.this, ChangePhoneActivity.class);
                startActivity(intent);
            }

            @Override
            protected void onFastClick() {

            }
        });
        relativeLayout.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                View view1 = LayoutInflater.from(IdSetActivity.this).inflate(R.layout.dialog_zhuxiao, null, false);
                final PopupWindow popupWindow = new PopupWindow(view1, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false);
                popupWindow.showAsDropDown(relativeLayout, 80, 70);
                View viewById = popupWindow.getContentView().findViewById(R.id.tv_voushu);
                WebView webView = popupWindow.getContentView().findViewById(R.id.webview_zhenren);
                View btn_zhuxiao = popupWindow.getContentView().findViewById(R.id.btn_todinish);
                TextView zhenrenbu = popupWindow.getContentView().findViewById(R.id.tv_zhenren);
                ImageView viewById1 = popupWindow.getContentView().findViewById(R.id.img_chadiao);
                zhenrenbu.setOnClickListener(new CustomClickListener() {
                    @Override
                    protected void onSingleClick() {
                        popupWindow.dismiss();
                    }

                    @Override
                    protected void onFastClick() {

                    }
                });
                viewById1.setOnClickListener(new CustomClickListener() {
                    @Override
                    protected void onSingleClick() {
                        popupWindow.dismiss();
                    }

                    @Override
                    protected void onFastClick() {

                    }
                });
                webView.loadUrl("https://mlhtml-test.dhxwlkj.com/logoffPolicy/");
                btn_zhuxiao.setOnClickListener(new CustomClickListener() {
                    @Override
                    protected void onSingleClick() {
                        getlogoff(EXTRA, MmkvUtil.INSTANCE.decodeString(TOKEN));
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode(TOKEN, "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode(ACCID, "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode(IMTOKEN, "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode(HOSTAUDIT, "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode(USER_NAME, "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode("phone", "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode(USERID, "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode("datingModeId", "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode("emotioRelationId", "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode("targetAgeId", "");
                        com.luoxiang.socialandroid.utils.MmkvUtil.INSTANCE.encode("sexpait", "");
                        IMKitClient.logoutIM(new LoginCallback<Void>() {
                            @Override
                            public void onError(int i, @NonNull String s) {
                                ToastUtils.showShort(s);
                            }

                            @Override
                            public void onSuccess(@Nullable Void unused) {

                            }
                        });
                        try {
                            ActivityUtils.finishActivity(HomeActivity.class);
                            ActivityUtils.finishActivity(SetActivity.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LoginUtil.Companion.initLogin(
                                IdSetActivity.this,
                                result -> {
                                    LoginUtil.Companion.startLogin(IdSetActivity.this, result);
                                    finish();
                                    return null;
                                },
                                () -> {
                                    showLoading();
                                    return null;
                                },
                                () -> {
                                    clearLoading();
                                    return null;
                                });

                    }

                    @Override
                    protected void onFastClick() {

                    }
                });
            }

            @Override
            protected void onFastClick() {

            }
        });
    }

    /**
     * 用户注销的方法
     */
    private void getlogoff(String extra, String token) {
        NetUtils.getInstance().getApiService().getlogoff(extra, token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        com.blankj.utilcode.util.LogUtils.i("decryptStrzhuxiao", decryptStr);

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}